// 资源文件加载器，确保canvas 在图片资源加载完再进行渲染

import {Resources} from "./resources.js"

export class ResourceLoader {
  map = null; // 浏览器不兼容这种写法
  constructor(){
    // es6 map 方法
    this.map = new Map(Resources)
    // 遍历resource 给每个resource 从
    // {key:"string"} => {key:object}
    for(let [key,value] of this.map){
      let image = wx.createImage();
      image.src = value;
      this.map.set(key,image)
    }
  }
  // 图片的onload 下载
  onLoaded(callback){
    let loadedCount = 0;
    for (let value of this.map.values()){
      value.onload = () => {
        loadedCount++;
        if(loadedCount >= this.map.size){
          callback(this.map)
        }
      }
    }
  }

  static create(){
    return new ResourceLoader();
  }
}